package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysClientRobot;
import com.ruoyi.system.domain.vo.SysClientRobotListVo;
import com.ruoyi.system.domain.vo.SysClientVo;
import com.ruoyi.system.mapper.SysClientRobotMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysClientMapper;
import com.ruoyi.system.domain.SysClient;
import com.ruoyi.system.service.ISysClientService;

/**
 * 仓库用户管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-10
 */
@Service
public class SysClientServiceImpl implements ISysClientService 
{
    @Autowired
    private SysClientMapper sysClientMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysClientRobotMapper clientRobotMapper;
    /**
     * 查询仓库用户管理
     * 
     * @param id 仓库用户管理主键
     * @return 仓库用户管理
     */
    @Override
    public SysClient selectSysClientById(Long id)
    {
        return sysClientMapper.selectSysClientById(id);
    }

    /**
     * 查询仓库用户管理列表
     * 
     * @param sysClient 仓库用户管理
     * @return 仓库用户管理
     */
    @Override
    public List<SysClient> selectSysClientList(SysClient sysClient)
    {
        List<SysClient> sysClients = sysClientMapper.selectSysClientList(sysClient);
        List<SysClientRobot> sysClientRobots = clientRobotMapper.selectSysClientRobotList(new SysClientRobot());



        if (CollUtil.isNotEmpty(sysClientRobots)){

            Map<Long, List<SysClientRobot>> collect = sysClientRobots.stream().collect(Collectors.groupingBy(SysClientRobot::getClientId));


            if (CollUtil.isNotEmpty(sysClients)){

                for (SysClient client : sysClients) {
                    for (Long clientId : collect.keySet()){
                        if (clientId.equals(client.getId())){
                            client.setRobots(collect.get(clientId));
                        }
                    }
                }

            }else {
                return sysClients;
            }
        }
        return sysClients;
    }

    /**
     * 新增仓库用户管理
     * 
     * @param sysClient 仓库用户管理
     * @return 结果
     */
    @Override
    public int insertSysClient(SysClient sysClient)
    {
        sysClient.setCreateTime(DateUtils.getNowDate());
        sysClient.setRegisteredTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        int i = sysClientMapper.insertSysClient(sysClient);
        if (i>0){
            SysUser sysUser = new SysUser();
            sysUser.setEmail(sysClient.getEmailAddr());
            sysUser.setPassword(sysClient.getPassword());
            sysUser.setUserName(sysClient.getClientName());
            sysUser.setNickName(sysClient.getClientName());
            sysUser.setClientId(sysClient.getId());
            sysUser.setType(1);
            sysUserMapper.insertUser(sysUser);
        }
        return i;
    }

    /**
     * 修改仓库用户管理
     * 
     * @param vo 仓库用户管理
     * @return 结果
     */
    @Override
    public int updateSysClient(SysClientVo  vo)
    {
        SysClient sysClient = sysClientMapper.selectSysClientById(vo.getId());
        sysClient.setWalletBalance(vo.getWalletBalance());
        sysClient.setWalletStatus(vo.getWalletStatus());
        sysClient.setRobotNum(vo.getRobotNum());
        sysClient.setRobotExTime(vo.getRobotExTime());
        return sysClientMapper.updateSysClient(sysClient);
    }

    /**
     * 批量删除仓库用户管理
     * 
     * @param ids 需要删除的仓库用户管理主键
     * @return 结果
     */
    @Override
    public int deleteSysClientByIds(Long[] ids)
    {
        return sysClientMapper.deleteSysClientByIds(ids);
    }

    /**
     * 删除仓库用户管理信息
     * 
     * @param id 仓库用户管理主键
     * @return 结果
     */
    @Override
    public int deleteSysClientById(Long id)
    {
        return sysClientMapper.deleteSysClientById(id);
    }


    @Override
    public boolean checkEmailAddrUnique(SysClient client)
    {
        Long userId = StringUtils.isNull(client.getId()) ? -1L : client.getId();
        SysClient info = sysClientMapper.checkUserNameUnique(client.getEmailAddr());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public boolean registerClient(SysClient sysClient) {

            return sysClientMapper.insertSysClient(sysClient) > 0;
    }

    @Override
    public int sysClientAddWalletKey(String key) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);

        Long clientId = sysUser.getClientId();

        SysClient sysClient = sysClientMapper.selectSysClientById(clientId);

        sysClient.setWalletKey(key);
        sysClient.setWalletStatus(0);
        sysClient.setUpdateTime(new Date());

        return sysClientMapper.updateSysClient(sysClient);

    }

    @Override
    public List<SysClientRobotListVo> selectSysClientListVo(SysClient sysClient) {

        List<SysClient> sysClients = sysClientMapper.selectSysClientList(sysClient);

        List<SysClientRobotListVo> vos = new ArrayList<>(sysClients.size());

        if (!CollUtil.isEmpty(sysClients)){
            for (SysClient client : sysClients) {

                SysClientRobotListVo vo = new SysClientRobotListVo();
                BeanUtil.copyProperties(client,vo);
                vos.add(vo);
            }
        }


        return vos;
    }
}
